﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using imaginecup.information;
using imaginecup.objectManager;
using Imaginecup.gameState.malaria.componentInit;
using imaginecup.util;
using Microsoft.Xna.Framework;
using Imaginecup.sprite.animation;
using Imaginecup.sprite.factory;
using Imaginecup.sprite;

namespace Imaginecup.gameState.malaria.component.entity.effect
{
    public class ShieldEffect: BaseEntityComponent
    {
        private ICmpChar m_Char;
        private AnimationIndexMan m_AnimationMan;

        public override void Release()
        {
        }


        public ShieldEffect(GObjectID a_GID, ObjectManager objMan, ICmpChar cmpChar)
            : base(a_GID, objMan, ComponentTypeID.CID_OBSERVER_CHAR)
        {
            m_Char = cmpChar;

            m_Info = new EntityInfo();

            m_Info.mSize = new Vector2(97, 85);
            EntityInfo charInfo = (EntityInfo)m_Char.GetEntityInfo();
            m_Info.SetEntityCenter(charInfo.GetEntityCenter());
        }


        public override void Init()
        {
            m_AnimationMan = new AnimationIndexMan();
            m_AnimationMan.SetIndex(Vector2.Zero, 6);
            m_AnimationMan.SetDelay(0.1);

            m_AnimationMan.StartAnimation();

            FactorySpriteInfo spriteInfo = new FactorySpriteInfo(SPRITE_TYPE_ID.ANIMATION_SPRITE, "ShieldEffect", m_AnimationMan);
            SpriteFactory spriteFactory = new SpriteFactory();
            
            ICmpRender render = new CmpDefaultRender(m_ObjectID, m_ObjectMan, (ISprite)spriteFactory.GetObject(spriteInfo), ComponentTypeID.CID_RENDER_TOP);
            m_ObjectMan.AddComponent(render);

            base.Init();
        }



        public override void Update(GameTime gameTime)
        {
            if (m_Char != null)
            {
                EntityInfo charInfo = (EntityInfo)m_Char.GetEntityInfo();
                m_Info.SetEntityCenter(charInfo.GetEntityCenter());

                m_AnimationMan.Update(gameTime);
                if (m_Char.GetShieldState() == false)
                {
                    m_ObjectMan.DestroyObject(m_ObjectID);
                    return;
                }
            }
            base.Update(gameTime);
        }
        public override EMessageResult HandleMessage(GMessage a_Msg)
        {
            if( a_Msg.m_Type.MessageTypeID == MessageTypeID.MT_CHILD_DIE.MessageTypeID)
            {
                if (m_Char.GetObjectID() == a_Msg.m_FromID)
                {
                    m_Char = null;
                    m_ObjectMan.DestroyObject(m_ObjectID);
                    return EMessageResult.MR_IGNORED;
                }
            }
            return base.HandleMessage(a_Msg);
        }
    }
}
